<?php
namespace Home\Controller;
use Think\Controller;
class AreaController extends Controller {
	

	/**
     * [index 地区表]
     * @return [type] [xml]
     */
	public function index(){
		$data = F('area_arr');
		//F('area_arr',NULL); 
		if(!$data){
			$Area = M('Area');
			$pro = $Area->where('area_deep = 1')->select();
			$city = $Area->where('area_deep = 2')->select();
			$area = $Area->where('area_deep = 3')->select();
			$pro_name = $pro['area_name'];
			$pro_id = $pro['area_id'];
			
			$arr = array();
			foreach($pro as $k=>$v){
				$j = 0;$n = 0;
				$pro_id = $v['area_id'];
				$pro_name = $v['area_name'];
				$arr[$k]['area_id'] = $pro_id;
				$arr[$k]['area_name'] = $pro_name;
				$arr[$k]['area_list'] = array();
				
				foreach($city as $v2){
					if($v['area_id'] == $v2['area_parent_id']){
						$arr[$k]['area_list'][$j]['area_id'] = $v2['area_id'];
						$arr[$k]['area_list'][$j]['area_name'] = $v2['area_name'];
						$arr[$k]['area_list'][$j]['area_list'] = array();
						foreach($area as $v3){
							if($v2['area_id'] == $v3['area_parent_id']){
								$arr[$k]['area_list'][$j]['area_list'][$n]['area_id'] = $v3['area_id'];
								$arr[$k]['area_list'][$j]['area_list'][$n]['area_name'] = $v3['area_name'];
								$n++;
							}
						}
						$j++;
					}
				}
			}	
			$data = $arr;
			F('area_arr',$data);
		}
			
		return $data;	
		
	}
	
	public function xml(){
		header("Content-type: text/xml");  
		echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";  
		echo "<root>";
		$arr = $this->index();
		foreach($arr as $v){
			echo "<province name='{$v['area_name']}' id='{$v['area_id']}'>";
			foreach($v['area_list'] as $v2){
				echo "<city name='{$v2['area_name']}' id='{$v2['area_id']}'  >";
					foreach($v2['area_list'] as $v3){
						echo " <district name='{$v3['area_name']}' id='{$v3['area_id']}' />";
					}
				echo "</city>";
			}
			echo " </province>";
		}
		echo "</root>";
	}


	
}

